package com.sms.controller;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.sms.common.A;
import com.sms.pojo.Promotions;
import com.sms.service.impl.PromotionsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

//优惠搜索
@WebServlet("/PromotionsSearchById")
public class PromotionsSearchByIdController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int promotionID=Integer.parseInt(req.getParameter("promotionID"));

        PromotionsServiceImpl service=new PromotionsServiceImpl();
        List<Promotions> promotions=service.listByIds(Collections.singleton(promotionID));
        service.getSqlSession().close();
        if (promotions!=null) {
            resp.getWriter().write(JSONUtil.toJsonStr(A.success("优惠搜索搜索成功", promotions), JSONConfig.create().setDateFormat("yyyy-MM-dd")));
        } else {
            resp.getWriter().write(JSONUtil.toJsonStr(A.fail("优惠搜索失败")));
        }
    }
}
